﻿using System;
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
namespace CxExtension
{
	public static class EnumerableUtility
	{
		public static T Min<T>(this IEnumerable<T> list,Func<T,float> func,out float minf)
		{
			T t = default(T);
			minf = float.MaxValue;
			float curf = 0;
			foreach(var item in list)
			{
				curf = func(item);
				if(curf < minf)
				{
					minf = curf;
					t = item;
				}
			}
			return t;
		}
		public static T Min<T>(this IEnumerable<T> list,Func<T,float> func)
		{
			float minf;
			return list.Min(func,out minf);
		}
		public static T Min<T>(this IEnumerable<T> list,Func<T,int> func)
		{
			int min;
			return list.Min(func,out min);
		}
		public static T Min<T>(this IEnumerable<T> list,Func<T,int> func,out int minf)
		{
			T t = default(T);
			minf = int.MaxValue;
			int curf = 0;
			foreach(var item in list)
			{
				curf = func(item);
				if(curf < minf)
				{
					minf = curf;
					t = item;
				}
			}
			return t;
		}
		public static T Max<T>(this IEnumerable<T> list,Func<T,float> func,out float maxf)
		{
			T t = default(T);
			maxf = float.MinValue;
			float curf = 0;
			foreach(var item in list)
			{
				curf = func(item);
				if(curf > maxf)
				{
					maxf = curf;
					t = item;
				}
			}
			return t;
		}
		public static T Max<T>(this IEnumerable<T> list,Func<T,float> func)
		{
			float max;
			return list.Max(func,out max);
		}
		public static T FindA<T>(this IEnumerable<T> self,Predicate<T> func)
		{
			T t = default(T);
			foreach(var item in self)
			{
				if(func(item))
				{
					t = item;
					break;
				}
			}
			return t;
		}
	}
}
